package com.nami.client.client2;

import com.nami.client.qos.QosReceiver;
import com.nami.client.request.LoginRequest;
import com.nami.client.request.ServerConfig;
import com.nami.client.service.ClientService;
import com.nami.client.service.impl.ClientServiceImpl;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

@Slf4j
public class ClientStartup {


    public static void main(String[] args) throws Exception {

        ServerConfig config = new ServerConfig();
        config.setHost("192.168.223.33");
//        config.setHost("127.0.0.1");
        config.setPort(30000);

        List<ClientService> clients = new ArrayList<ClientService>();

        ExecutorService executor = Executors.newFixedThreadPool(20);

        List<Callable<ClientService>> callables = new ArrayList<Callable<ClientService>>();
        for (int i = 300; i < 401; i++) {
            final int index = i;
            callables.add(() -> {
                LoginRequest request = new LoginRequest();
                request.setAppKey("test");
                request.setDeviceType(13);
                request.setUserId(index + "");
                request.setCurrTime(System.currentTimeMillis());

                ClientService clientService = new ClientServiceImpl(config, request);
                clientService.start();
                return clientService;
            });

        }
        List<Future<ClientService>> futures = executor.invokeAll(callables);
        for (Future<ClientService> future : futures) {
            clients.add(future.get());
        }
        ExecutorService executorService = Executors.newFixedThreadPool(10);

        for (; ; ) {
            for (ClientService client : clients) {
                executorService.execute(
                    () -> {
                        client.send();
                    }
                );

            }
            Thread.sleep(1000);
            log.info("[----------------------------------------------------------------]");
        }

    }

}
